package com.cellulargames.view;



import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import com.cellulargames.control.Controller;
import com.cellulargames.control.Painter;
import com.cellulargames.listener.ICanvas;
import com.cellulargames.model.Font59;
import com.ramp.microswing.Graphics2D;

public class CoinDialog implements ICanvas{

	private static CoinDialog coinsDialog;
	
	protected int width;
	
	protected int height;
	
	protected int x;
	
	protected int y;
	
	private Font59 coins;
	
	private Font59 max;
	
	private int xcoin;
	
	private int ycoin;
	
	private int xcoinIcon;
	
	private int xmax;
	
	private int xarrow;
	
	private int yarrow;
	
	private int _yarrow;
	
	private int ycoinIcon;
	
	private int currApuesta = 1;
	
	private String maxMin;

	private boolean show;
	
	protected CoinDialog(){
		super();
		
		width = Painter.getInstance().getWidth();
		height = 53;
		y = Painter.getInstance().getHeight() - height - (height/2);
		
		coins = new Font59("/font.fnt");
		max = new Font59("/font.fnt");
		
		xcoin = (int)(width*0.88);		
		
		xcoinIcon = (int)(width*0.81);
		ycoinIcon = (int)(Painter.getInstance().getHeight()*0.83);
		
		ycoin = (int)(Painter.getInstance().getHeight()*0.82);
		
		xarrow = (int)(width*0.91);
		xmax = (int)(width*0.10);
		
		yarrow = (int)(Painter.getInstance().getHeight()*0.80);
		_yarrow = (int)(Painter.getInstance().getHeight()*0.88);
		
	}
	
	public static CoinDialog getInstance(){
		if( coinsDialog == null ){
			coinsDialog = new CoinDialog();
		}
		return coinsDialog;
	}
	
	public void show() {
		currApuesta = Controller.apuesta;
		Painter.setModalDialog(getInstance());
		maxMin = "Max: "+Controller.MAX_APUESTA+" - Min: "+Controller.MIN_APUESTA;
		show = true;
		Painter.getInstance().repaint();
	}
	
	public void close() {
		Painter.removeModalDialog();
		show = false;
	}
	
	public void paint(Graphics g) {
		
		if( show ){
		
			Graphics2D.drawIlumination(g, x, y, width, height);
			
			g.setClip(x, y, Painter.getInstance().getWidth(), Painter.getInstance().getHeight());
			
			g.setColor(0x333333);
			g.fillRect(x, y, width, height);
			
			g.drawImage(VideoPoker.coins[1], xcoinIcon, ycoinIcon, Graphics.LEFT|Graphics.TOP);		
			coins.PrintString(g, xcoin, ycoin, String.valueOf(currApuesta), Font59.LEFT, Font59.TOP);
						
			g.setColor(0xFFFFFF);
			Graphics2D.fillTriangle(g, xarrow, yarrow, 5, 5, Graphics2D.HEAD_UP);
			Graphics2D.fillTriangle(g, xarrow, _yarrow, 4, 4, Graphics2D.HEAD_DOWN);
			
			max.PrintString(g, xmax, ycoin, maxMin, Font59.LEFT, Font59.TOP);		
		
		}
		
	}
	
	public void keyPressed(int key) {

		switch ( Painter.getInstance().getGameAction(key) ) {
		
		case Canvas.UP:					
			if( currApuesta < Controller.MAX_APUESTA ){
				currApuesta++;
			}
			break;
			
		case Canvas.DOWN:
			if( currApuesta > Controller.MIN_APUESTA ){
				currApuesta--;
			}
			break;

		case Canvas.FIRE:
			if( currApuesta > 0 ){
				Controller.apuesta = currApuesta;
				close();
			}
			break;
			
		default:
			break;
		
		}
		
		Painter.getInstance().repaint();
		
	}
	
	public void keyReleased(int key) {
		
	}
	
}
